package com.power.sifa.info.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.info.model.Law_keyword;
import com.power.sifa.info.model.pageInfo.Law_keywordPageInfo;
import com.power.sifa.info.api.ILaw_keywordFeign;
import com.power.sifa.info.service.Law_keywordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   法律关键词控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Law_keywordController",tags={"法律关键词"})
public class Law_keywordController implements ILaw_keywordFeign{

    @Resource
    private Law_keywordService law_keywordService;

    
    @Override
    @ApiOperation("查询法律关键词分页列表")
    @SaCheckPermission("law_keyword_list")
    public ResponseDTO list(Law_keywordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Law_keyword> page = this.law_keywordService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Law_keyword law_keyword : page) {
                law_keyword.geneText();
                list.add(law_keyword);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看法律关键词")
    @SaCheckPermission("law_keyword_view")
    public ResponseDTO view(Integer id) {
        Assert.notNull(id, "id不能为空");
        Law_keyword law_keyword  = law_keywordService.getById(id);
        if (law_keyword != null) {
            law_keyword.geneText();
        }
        return ResponseDTO.succData(law_keyword);
    }

    @Override
    @ApiOperation("删除法律关键词")
    @SaCheckPermission("law_keyword_del")
    public ResponseDTO delete(Integer id) {
        Assert.notNull(id, "id不能为空");
        law_keywordService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改法律关键词")
    @SaCheckPermission("law_keyword_save")
    public ResponseDTO save(Law_keyword law_keyword) {
        Assert.notNull(law_keyword, "law_keyword不能为空");
        law_keywordService.saveOrUpdate(law_keyword);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除法律关键词")
    @SaCheckPermission("law_keyword_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        law_keywordService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("law_keyword_list")
    public void output(HttpServletResponse response, Law_keywordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "法律关键词";
        String[] heads = {"序号","法律编码","条文编码","关键词","类型","删除标志"};
        String[] fields = {"id","docid","provisionid","word","type","deleted"};
        Page<Law_keyword> page = this.law_keywordService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "法律关键词";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","法律编码","条文编码","关键词","类型","删除标志"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","docid","provisionid","word","type","deleted"};
        List<Law_keyword> list = (List<Law_keyword>)ExcelUtil.Import(fields, file, Law_keyword.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.law_keywordService.saveAll(list);
        }
        return ResponseDTO.succ();
	}
    
}
